package net.alcuria.ball;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import net.alcuria.framework.Game;
import net.alcuria.framework.Graphics;
import net.alcuria.framework.Input.TouchEvent;
import net.alcuria.framework.gl.Animation;
import net.alcuria.framework.gl.Camera2D;
import net.alcuria.framework.gl.SpriteBatcher;
import net.alcuria.framework.impl.GLScreen;
import net.alcuria.framework.math.Rectangle;
import net.alcuria.framework.math.Vector2;
import net.alcuria.framework.Screen;

public class MapViewerScreen extends GLScreen {
	Camera2D guiCam;
	SpriteBatcher batcher;
	float stateTime = 0.0F;
	Snapshot theCamera = new Snapshot(0, 0, 544, 320, Assets.map);
	

//	Ball player = new Ball(7*32, 28*32);
//	Director particleDirector = new Director();

	int screenX, screenY, diffY = 0;
	int numSkyTiles, skyWidth;
	int numParallaxTiles, parallaxWidth;
	int numForegroundTiles, foregroundWidth;
	int playerX, playerY;
	int tileWidth, sheetWidth, tileData;

	int animationTick = 0;
	int animationFrame = 0;

	public MapViewerScreen(Game game){
		super(game);
		guiCam = new Camera2D(glGraphics, 544, 320);
		batcher = new SpriteBatcher(glGraphics, 1000);
		
		theCamera.move(7*32, 28*32);

		numSkyTiles = (int) (544 / Assets.parallaxSky.width+1);
		skyWidth = (int) (Assets.parallaxSky.width) - 5;
		numParallaxTiles = (int) (544 / Assets.parallaxTrees.width + 1);
		parallaxWidth = (int) (Assets.parallaxTrees.width) - 5;
		numForegroundTiles = (int) (544 / Assets.parallaxForeground.width + 1);
		foregroundWidth = (int) (Assets.parallaxForeground.width);

		playerX = 0;
		playerY = 0;
	}


	public void update(float deltaTime) {

		theCamera.move(theCamera.x + 2, (int) (Math.sin(stateTime) * 300));
		stateTime += deltaTime;
		/*
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		
		// handle user inputs
		int len = touchEvents.size();
		for(int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if(event.type == TouchEvent.TOUCH_DOWN && player.fallTimer < 4){
				player.jump();
				Assets.jump.play(1);
			}

		}

		// update camera
		theCamera.update(player.x, player.y, player.onSurface);

		// handle player/object update
		player.update(deltaTime, particleDirector);

		// update tile animations
		animationTick++;
		if(animationTick > 10){
			animationFrame++;
			animationFrame = animationFrame % 4;
			animationTick = 0;
		}
			
		// update particles
		particleDirector.update();
		 */
		
	}


	@Override
	public void present(float deltaTime) {
		
		stateTime += deltaTime;
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.setViewportAndMatrices();

		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.glParallax);
		for(int i = 0; i <= numSkyTiles; i++) {
			batcher.drawSprite(i*skyWidth - (theCamera.x/8) % skyWidth,  theCamera.y/12 + 160, Assets.parallaxSky.width, Assets.parallaxSky.height, Assets.parallaxSky);
			
		}
		for(int i = 0; i <= numParallaxTiles; i++) {
			batcher.drawSprite(i*parallaxWidth - (theCamera.x/4) % parallaxWidth,  theCamera.y/8 + 100, Assets.parallaxTrees.width, Assets.parallaxTrees.height, Assets.parallaxTrees);

		}
		batcher.endBatch();
		
		
		// map time -- gl version
		tileWidth = Assets.map.tileWidth;
		sheetWidth = Assets.map.sheetWidth;
		tileData = 0;

		// lower layer
		
		batcher.beginBatch(Assets.glTileset);
		for(int row = 0; row <= theCamera.numTilesY; row++){
			for(int col = 0; col <= theCamera.numTilesX; col++){
				tileData = Assets.map.lowerMap[col + theCamera.left][row + theCamera.top];
				if( tileData > 0) {			
					batcher.drawSprite(col * 32 + theCamera.dispX,
							(theCamera.numTilesY - row - 1) * 32 - theCamera.dispY,
							32.0f,
							32.0f,
							Assets.tileset[tileData]);
					
					
					if(tileData > 96 && tileData < 99){
						Assets.map.lowerMap[col + theCamera.left][row + theCamera.top]++;
					}
					if (tileData >= 100 && tileData < 104){
						Assets.map.lowerMap[col + theCamera.left][row + theCamera.top] = 100 + animationFrame;
					}

					
					// spring
					if (tileData > 156 && tileData < 160){
						Assets.map.lowerMap[col + theCamera.left][row + theCamera.top]++;
						if (tileData == 159){
							Assets.map.lowerMap[col + theCamera.left][row + theCamera.top] = 156;
						}
					}
					if (tileData > 148 && tileData < 152){
						Assets.map.lowerMap[col + theCamera.left][row + theCamera.top]++;
						if (tileData == 151){
							Assets.map.lowerMap[col + theCamera.left][row + theCamera.top] = 148;
						}
					}

				}
			}
		}
		batcher.endBatch();
		
		
		gl.glDisable(GL10.GL_BLEND);
		
		
		
		////--------------------------------------------------------------------
		
		// Parallax

/*


		// object-level assets
		playerX = (int)player.x - theCamera.x;
		playerY = (int)player.y - theCamera.y;

		g.drawPixmap(player.character.spritesheet, playerX, playerY, player.character.srcX, player.character.srcY, player.character.frameWidth, player.character.frameHeight);
		g.drawPixmap(player.ball.spritesheet, playerX, playerY, player.ball.srcX, player.ball.srcY, player.ball.frameWidth, player.ball.frameHeight);

		// draw all particles
		for(int i=0; i<particleDirector.spriteList.size(); i++){
			g.drawPixmap(particleDirector.spriteList.get(i).particle.spritesheet, 
					particleDirector.spriteList.get(i).x - theCamera.x, 
					particleDirector.spriteList.get(i).y - theCamera.y, 
					particleDirector.spriteList.get(i).particle.srcX, 
					particleDirector.spriteList.get(i).particle.srcY, 
					particleDirector.spriteList.get(i).particle.frameWidth, 
					particleDirector.spriteList.get(i).particle.frameHeight);
			
		}
		
		// upper layer
		for(int row = 0; row < theCamera.numTilesY; row++){
			for(int col = 0; col < theCamera.numTilesX; col++){
				tileData = Assets.map.upperMap[col + theCamera.left][row + theCamera.top];
				if( tileData > 0) {
					g.drawPixmap(Assets.tileset,
							col * tileWidth + theCamera.dispX, 
							row * tileWidth + theCamera.dispY,
							(tileData % sheetWidth) * tileWidth, 
							(tileData / sheetWidth) * tileWidth, 
							tileWidth, 
							tileWidth);
					if (tileData >= 120 && tileData < 124){
						Assets.map.upperMap[col + theCamera.left][row + theCamera.top] = 120 + animationFrame;
					}
					if (tileData >= 152 && tileData < 156){
						Assets.map.upperMap[col + theCamera.left][row + theCamera.top] = 152 + animationFrame;
					}

				}
			}
		}

		// Foreground parallax
		for(int i = 0; i <= numForegroundTiles; i++) {
			g.drawPixmap(Assets.foreground, (i * foregroundWidth) - (int)(theCamera.x*1.6) % foregroundWidth, 370 - theCamera.y/6);
		}
		*/

	}



	@Override
	public void pause() {
		Settings.save(game.getFileIO());

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {

	}

}
